/**
 * @Title: model.js
 * @Description: model
 * @author: Fanchao
 * @date: 2020/8/2 11:10 上午
 * @version V1.0
 */

import { pathToRegexp } from 'path-to-regexp';
import { message } from 'antd';
import {
    createEvaluateObject,
    getPagedEvaluateObjectModel,
    getTeacherUserModelList,
    changeEvaluateObjectInfo,
    deleteEvaluateObject,
} from 'api/index';

export default {
    namespace: 'theObjects',
    state: {
        dataSource: {},
        keyword: '',
        reqPageInfo: {
            pageNumber: 1,
            pageSize: 10,
        },
        modalTitle: '新增领导',
        modalVisible: false,
        itemData: {},
        teacherOptions: [],
    },
    reducers: {
        setDataSource(state, { payload: { dataSource, keyword, reqPageInfo } }) {
            return {
                ...state,
                dataSource,
                keyword,
                reqPageInfo,
            };
        },
        setModalVisible(state, { payload: modalVisible }) {
            return { ...state, modalVisible };
        },
        setModalTitle(state, { payload: modalTitle }) {
            return { ...state, modalTitle };
        },
        setItemData(state, { payload: itemData }) {
            return { ...state, itemData };
        },
        setTeacherOptions(state, { payload: teacherOptions }) {
            return { ...state, teacherOptions };
        },

    },
    effects: {
        /**
         * @desc 获取评定对象列表
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* getDataSource({ payload }, { call, put, select }) {
            const result = yield call(getPagedEvaluateObjectModel, payload);
            if (result.status === 'success') {
                const dataSource = result.result;
                yield put({
                    type: 'setDataSource',
                    payload: {
                        dataSource,
                        keyword: payload.keyword,
                        reqPageInfo: payload.reqPageInfo,
                    },
                });
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 教员列表数据
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* getTeacherOptionList({ payload }, { call, put, select }) {
            const result = yield call(getTeacherUserModelList, payload);
            if (result.status === 'success') {
                const teacherOptions = result.result;
                yield put({
                    type: 'setTeacherOptions',
                    payload: teacherOptions,
                });
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 创建评定对象
         * @param payload
         * @param callback
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* addEvaluateObject({ payload, callback }, { call, put, select }) {
            const result = yield call(createEvaluateObject, payload);
            if (result.status === 'success') {
                message.success(`添加成功`);
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 删除评定对象
         * @param payload
         * @param callback
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* removeEvaluateObject({ payload, callback }, { call, put, select }) {
            const result = yield call(deleteEvaluateObject, payload);
            if (result.status === 'success') {
                message.success(`删除成功`);
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 修改评定对象
         * @param payload
         * @param callback
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* updateEvaluateObjectInfo({ payload, callback }, { call, put, select }) {
            const result = yield call(changeEvaluateObjectInfo, payload);
            if (result.status === 'success') {
                message.success(`修改成功`);
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },

    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname }) => {
                if (pathToRegexp('/evaluations/the-objects').exec(pathname)) {
                }
            });
        },
    },
};
